﻿/* Autor: Achim Schnell */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using AppFx.ComponentModel.Descriptive;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace Tests.ComponentModel
{
    /// <summary>
    ///Dies ist eine Testklasse für "Test_ITypeDescriptionProvider" und soll
    ///alle Test_ITypeDescriptionProvider Komponententests enthalten.
    ///</summary>
	public class Test_ITypeDescriptionProvider
	{
		public ITypeDescriptionProvider Provider { get; set; }

		internal virtual ITypeDescriptionProvider CreateITypeDescriptionProvider()
		{
			if (Provider == null)
				throw new NullReferenceException("Der TypeDescriptionProvider 'Provider' für die Testklasse Test_ITypeDescriptionProvider wurde nicht nicht initialisiert. (Ist NULL)");
			return Provider;
		}

		/// <summary>
		///Ein Test für "GetDescriptors"
		///</summary>
		public void Can_GetDescriptorsByTypeTest()
		{
			ITypeDescriptionProvider target = CreateITypeDescriptionProvider();
			Type type = typeof(CustomType);
			IList<PropertyDescriptor> actual = target.GetDescriptors(type);
			Assert.IsNotNull(actual.Where(p => p.Name == "Title"));
			Assert.IsNotNull(actual.Where(p => p.Name == "Name"));
			Assert.IsNotNull(actual.Where(p => p.Name == "LastName"));
		}

		/// <summary>
		///Ein Test für "GetDescriptors"
		///</summary>
		public void Can_GetDescriptorsByInstanceTest()
		{
			ITypeDescriptionProvider target = CreateITypeDescriptionProvider();
			object instance = new CustomType();
			IList<PropertyDescriptor> actual = target.GetDescriptors(instance);
			Assert.IsNotNull(actual.Where(p => p.Name == "Title"));
			Assert.IsNotNull(actual.Where(p => p.Name == "Name"));
			Assert.IsNotNull(actual.Where(p => p.Name == "LastName"));
		}

		/// <summary>
		///Ein Test für "GetDescriptors"
		///</summary>
		public void Can_GetDescriptorsByTypeExclusion()
		{
			ITypeDescriptionProvider target = CreateITypeDescriptionProvider();
			Type type = typeof(CustomType);
			IList<PropertyDescriptor> descriptors = target.GetDescriptors(type,
				p => p.Name == "Title");
			Assert.IsFalse(descriptors.Where(p => p.Name == "Title").Count() > 0);
		}

		/// <summary>
		///Ein Test für "GetDescriptors"
		///</summary>
		public void Can_GetDescriptorsByInstanceExclusion()
		{
			ITypeDescriptionProvider target = CreateITypeDescriptionProvider();
			object instance = new CustomType();
			IList<PropertyDescriptor> descriptors = target.GetDescriptors(instance,
				p => p.Name == "Title");
			Assert.IsFalse(descriptors.Where(p => p.Name == "Title").Count() > 0);
		}

		/// <summary>
		///Ein Test für "GetDescriptors"
		///</summary>
		public void Can_GetDescriptorsByInstanceExclusions()
		{
			ITypeDescriptionProvider target = CreateITypeDescriptionProvider();
			target.Exclusions.Add(p => p.Name == "Title");
			target.Exclusions.Add(p => p.Name == "Name");
			object instance = new CustomType();
			IList<PropertyDescriptor> descriptors = target.GetDescriptors(instance);
			Assert.IsFalse(descriptors.Where(p => p.Name == "Title").Count() > 0);
			Assert.IsFalse(descriptors.Where(p => p.Name == "Name").Count() > 0);
			target.Exclusions.Clear();
		}

		/// <summary>
		///Ein Test für "GetDescriptors"
		///</summary>
		public void Can_GetDescriptorsByTypeExclusions()
		{
			ITypeDescriptionProvider target = CreateITypeDescriptionProvider();
			target.Exclusions.Add(p => p.Name == "Title");
			target.Exclusions.Add(p => p.Name == "Name");
			Type type = typeof(CustomType);
			IList<PropertyDescriptor> descriptors = target.GetDescriptors(type);
			Assert.IsFalse(descriptors.Where(p => p.Name == "Title").Count() > 0);
			Assert.IsFalse(descriptors.Where(p => p.Name == "Name").Count() > 0);
			target.Exclusions.Clear();
		}
	}

	public class CustomType
	{
		public string Title { get; set; }

		public string Name { get; set; }

		public string LastName { get; set; }
	}
}
